/**
 * Created by zhaojm on 15/3/17.
 */
game.Coin = cc.Node.extend({
    _matrix : null,
    _subCoinList : null,
    _subCoinSize : null,
    ctor:function(matrix){
        this._super();

        this._matrix = matrix;

        this.initCoin();
    },

    initCoin : function(){
        //var matrix = [
        //    1, 1, 0, 1, 1,
        //    1, 1, 1, 1, 1,
        //    1, 1, 1, 1, 1,
        //    1, 0, 0, 0, 1
        //];
        //var x = 5;
        //var y = 4;

        this._subCoinList = [];

        var numlist = this._matrix.numlist;
        var x = this._matrix.x;
        //var y = this._matrix.y;
        var y = 0;

        var zero = 0;
        for(var i = numlist.length - 1; i >= 0; i--){
            if(y < this._matrix.y) {
                if(x == 0) {
                    x = this._matrix.x;
                    y++;
                }

                //cc.log('x=', x, 'y=', y, 'i=', i);
                // TODO 判断 并 显示 numlist[i]
                // 坐标 (x, y)
                if(numlist[i] == 1){
                    var subCoin = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('coin.png'));
                    subCoin.needCollided = true;
                    this._subCoinSize = subCoin.getContentSize();
                    this.addChild(subCoin);
                    this._subCoinList.push(subCoin);
                    subCoin.setPosition(x * this._subCoinSize.width, y * this._subCoinSize.height);
                }else{
                    zero++;
                    //cc.log('zero=', zero);
                }
                x--;

            }else {
                //cc.log('y <= 0');
                break;
            }
        }
        if(this._subCoinSize != null){
            this.setContentSize(new cc.Size(this._matrix.x * this._subCoinSize.width, this._matrix.y * this._subCoinSize.height));
        }else{

        }
    },


    checkCollided:function(heroRect){
        // TODO 首先检查是否和 这个金币块 碰撞
        var coinRect = cc.rect(
            this.getPositionX(),
            this.getPositionY(),
            this.getContentSize().width,
            this.getContentSize().height
        );

        if(this.isCollided(heroRect, coinRect)){
            //cc.log('Collided with coin');
            // TODO 检测每个金币的碰撞

            for(var i = 0; i < this._subCoinList.length; i++){
                var subCoin = this._subCoinList[i];
                if(subCoin.needCollided == false){
                    continue;
                }

                var subCoinRect = cc.rect(
                    this.getPositionX() + subCoin.getPositionX(),
                    this.getPositionY() + subCoin.getPositionY(),
                    subCoin.getContentSize().width,
                    subCoin.getContentSize().height
                );

                if(this.isCollided(heroRect, subCoinRect)){
                    //cc.log('Collided with subCoin');
                    // TODO 碰撞后，金币消失，分数增加

                    this._subCoinList.remove(subCoin);
                    subCoin.removeFromParent();
                    subCoin.needCollided = false;
                    this.parent.parent.addOneScore();
                }
            }
        }




    },

    isCollided:function(aRect,bRect){
        return cc.rectIntersectsRect(aRect, bRect);
    },


});


game.Matrix = {
    x:0,
    y:0,
    numlist:[],
};

game.Matrix.makeMatrix = function(x, y, numlist){
    var matrix = {
        x:x,
        y:y,
        numlist:numlist
    };
    return matrix;
};